
package com.chushouya.manager.dao.repository;

import com.chushouya.manager.dao.entity.ProxyEntity;
import com.chushouya.manager.dao.mapper.ProxyEntityMapper;
import com.chushouya.manager.dto.admin.proxy.ProxyQuery;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class ProxyRepository extends BaseRepository<ProxyEntityMapper, ProxyEntity, Long> {

    public ProxyEntity selectOne(ProxyQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    private Example buildExample(ProxyQuery query) {
        Example example = this.buildExample();
        if (Objects.nonNull(query.getProxyId())) {
            example.and().andEqualTo("proxyId", query.getProxyId());
        }
        if (Objects.nonNull(query.getAdminUserId())) {
            example.and().andEqualTo("adminUserId", query.getAdminUserId());
        }
        if (Strings.isNotEmpty(query.getProxyName())) {
            example.and().andLike("proxyName", Strings.format("%{}%", query.getProxyName()));
        }
        if (Strings.isNotEmpty(query.getContactPhone())) {
            example.and().andEqualTo("contactPhone", query.getContactPhone());
        }
        example.orderBy("createTime").desc();
        return example;
    }

    public PageResult<ProxyEntity> selectPageResult(ProxyQuery query) {
        return this.selectPage(query.getPageRequest(), this.buildExample(query));
    }

    public List<ProxyEntity> selectList(ProxyQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

    public List<ProxyEntity> selectPageList(ProxyQuery query) {
        return this.selectPageList(query.getPageRequest(), this.buildExample(query));
    }
}
